package com.shop.dao.shoppingcart;


import com.shop.dao.BaseDao;
import com.shop.pajo.ShoppingCart;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCartDaoImpl implements ShoppingCartDao {

    @Override
    public int addToCart(Connection connection, ShoppingCart shoppingCart) throws SQLException {
        PreparedStatement preparedStatement = null;
        int addNum = 0;

        if(connection != null) {
            String sql = "insert into shopping_cart " +
                    "values(?,?)";
            Object[] params = {shoppingCart.getUid(), shoppingCart.getBid()};

            addNum = BaseDao.execute(connection, preparedStatement, sql, params);

            BaseDao.closeResource(null, preparedStatement, null);
        }

        return addNum;
    }

    @Override
    public ShoppingCart getShoppingCartById(Connection connection, int uid, String bid) throws SQLException {
        PreparedStatement preparedStatement = null;
        ShoppingCart shoppingCart = null;
        ResultSet resultSet = null;

        if (connection != null) {
            String sql = "select * from shopping_cart " +
                    "where uid=? and bid=?";
            Object[] params = {uid, bid};
            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()) {
                shoppingCart = new ShoppingCart();
                shoppingCart.setUid(resultSet.getInt("uid"));
                shoppingCart.setBid(resultSet.getString("bid"));
            }

            BaseDao.closeResource(null, preparedStatement, resultSet);
        }
        return shoppingCart;
    }

    @Override
    public List<ShoppingCart> getShoppingCartByUid(Connection connection, int uid) throws SQLException {
        PreparedStatement preparedStatement = null;
        ShoppingCart shoppingCart = null;
        ResultSet resultSet = null;

        List<ShoppingCart> list = new ArrayList<ShoppingCart>();

        if (connection != null) {
            String sql = "select * from shopping_cart " +
                    "where uid=?";
            Object[] params = {uid};
            resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);

            while (resultSet.next()) {
                shoppingCart = new ShoppingCart();
                shoppingCart.setUid(resultSet.getInt("uid"));
                shoppingCart.setBid(resultSet.getString("bid"));
                list.add(shoppingCart);
            }

            BaseDao.closeResource(null, preparedStatement, resultSet);
        }
        return list;
    }

    @Override
    public int deleteCartByBid(Connection connection, String bid) throws SQLException {
        int deleteNum = 0;
        PreparedStatement preparedStatement = null;

        if(connection != null) {
            String sql = "delete from shopping_cart where bid=?";
            Object[] params = {bid};
            deleteNum = BaseDao.execute(connection, preparedStatement, sql, params);
            BaseDao.closeResource(null, preparedStatement, null);
        }

        return deleteNum;
    }

}
